﻿using System;
using System.ComponentModel;
using System.Drawing;
using HML.Design;

namespace HML
{

    /// <summary>
    /// 导航栏按钮
    /// </summary>
    [TypeConverter(typeof(PropertyOrderConverter))]
    public class TabControlPlusNavigationButton
    {
        #region 属性

        private TabControlPlusNavigationButtonStyles style = TabControlPlusNavigationButtonStyles.Default;
        /// <summary>
        /// 导航栏按钮风格
        /// </summary>
        [Description("导航栏按钮风格")]
        [PropertyOrder(-200)]
        [DefaultValue(TabControlPlusNavigationButtonStyles.Default)]
        public TabControlPlusNavigationButtonStyles Style
        {
            get { return this.style; }
            set
            {
                if (this.style == value)
                    return;

                this.style = value;
                this.Invalidate();
            }
        }

        private LRMargin lRMargin = new LRMargin(2, 2);
        /// <summary>
        /// 导航栏按钮左右外边距
        /// </summary>
        [Description("导航栏按钮左右外边距")]
        [PropertyOrder(-198)]
        [DefaultValue(typeof(LRMargin), "2,2")]
        public LRMargin LRMargin
        {
            get
            {
                return this.lRMargin;
            }
            set
            {
                if (this.lRMargin == value || value.Left < 0 || value.Right < 0)
                    return;

                this.lRMargin = value;
                this.InitializeInvalidate();
            }
        }

        private Size size = new Size(12, 12);
        /// <summary>
        /// 导航栏按钮Size
        /// </summary>
        [Description("导航栏按钮Size")]
        [PropertyOrder(-196)]
        [DefaultValue(typeof(Size), "12, 12")]
        public Size Size
        {
            get { return this.size; }
            set
            {
                if (this.size == value || value.Width < 0 || value.Height < 0)
                    return;

                this.size = value;
                this.InitializeInvalidate();
            }
        }

        private Color backNormalColor = Color.Empty;
        /// <summary>
        /// 导航栏按钮背景颜色(正常)
        /// </summary>
        [Description("导航栏按钮背景颜色(正常)")]
        [PropertyOrder(-194)]
        [DefaultValue(typeof(Color), "")]
        public Color BackNormalColor
        {
            get { return this.backNormalColor; }
            set
            {
                if (this.backNormalColor == value)
                    return;

                this.backNormalColor = value;
                if (this.Style == TabControlPlusNavigationButtonStyles.Default)
                {
                    this.Invalidate();
                }
            }
        }

        private Color backEnterColor = Color.Empty;
        /// <summary>
        /// 导航栏按钮背景颜色(鼠标进入)
        /// </summary>
        [Description("导航栏按钮背景颜色(鼠标进入)")]
        [PropertyOrder(-192)]
        [DefaultValue(typeof(Color), "")]
        public Color BackEnterColor
        {
            get { return this.backEnterColor; }
            set
            {
                if (this.backEnterColor == value)
                    return;

                this.backEnterColor = value;
                if (this.Style == TabControlPlusNavigationButtonStyles.Default)
                {
                    this.Invalidate();
                }
            }
        }

        private Color foreNormalColor = Color.FromArgb(153, 204, 204);
        /// <summary>
        /// 导航栏按钮前景颜色(正常)
        /// </summary>
        [Description("导航栏按钮前景颜色(正常)")]
        [PropertyOrder(-190)]
        [DefaultValue(typeof(Color), "153, 204, 204")]
        public Color ForeNormalColor
        {
            get { return this.foreNormalColor; }
            set
            {
                if (this.foreNormalColor == value)
                    return;

                this.foreNormalColor = value;
                if (this.Style == TabControlPlusNavigationButtonStyles.Default)
                {
                    this.Invalidate();
                }
            }
        }

        private Color foreEnterColor = Color.FromArgb(130, 178, 178);
        /// <summary>
        /// 导航栏按钮前景颜色(鼠标进入)
        /// </summary>
        [Description("导航栏按钮前景颜色(鼠标进入)")]
        [PropertyOrder(-188)]
        [DefaultValue(typeof(Color), "130, 178, 178")]
        public Color ForeEnterColor
        {
            get { return this.foreEnterColor; }
            set
            {
                if (this.foreEnterColor == value)
                    return;

                this.foreEnterColor = value;
                if (this.Style == TabControlPlusNavigationButtonStyles.Default)
                {
                    this.Invalidate();
                }
            }
        }

        private Image imageNormal = null;
        /// <summary>
        /// 导航栏按钮图片(正常)
        /// </summary>
        [Description("导航栏按钮图片(正常)")]
        [PropertyOrder(-186)]
        [DefaultValue(null)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public Image ImageNormal
        {
            get { return this.imageNormal; }
            set
            {
                if (this.imageNormal == value)
                    return;

                this.imageNormal = value;
                if (this.Style == TabControlPlusNavigationButtonStyles.Image)
                {
                    this.Invalidate();
                }
            }
        }

        private Image imageEnter = null;
        /// <summary>
        /// 导航栏按钮图片(鼠标进入)
        /// </summary>
        [Description("导航栏按钮图片(鼠标进入)")]
        [PropertyOrder(-184)]
        [DefaultValue(null)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public Image ImageEnter
        {
            get { return imageEnter; }
            set
            {
                if (this.imageEnter == value)
                    return;

                this.imageEnter = value;
                if (this.Style == TabControlPlusNavigationButtonStyles.Image)
                {
                    this.Invalidate();
                }
            }
        }

        #region 临时存储

        /// <summary>
        /// 导航栏按钮rect(包括外边距)
        /// </summary>
        [Description("导航栏按钮rect(包括外边距)")]
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        protected internal Rectangle M_Rect { get; set; }

        /// <summary>
        /// 导航栏按钮rect(不包括外边距、边框、内边距)
        /// </summary>
        [Description("导航栏按钮rect(不包括外边距、边框、内边距)")]
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        protected internal Rectangle C_Rect { get; set; }

        #endregion

        #endregion

        #region 字段

        /// <summary>
        /// 导航栏按钮所属TabControl
        /// </summary>
        private TabControlPlus owner;

        #endregion

        /// <summary>
        /// 导航栏按钮所属TabControl
        /// </summary>
        /// <param name="_owner"></param>
        public TabControlPlusNavigationButton(TabControlPlus _owner)
        {
            this.owner = _owner;
        }

        #region 私有方法

        /// <summary>
        /// 控件重新初始化和重绘
        /// </summary>
        private void InitializeInvalidate()
        {
            if (this.owner != null)
            {
                TabControlPlus tabControl = (TabControlPlus)this.owner;
                tabControl.InitializeRectangle();
                tabControl.Invalidate();
            }
        }

        /// <summary>
        /// 控件重绘
        /// </summary>
        private void Invalidate()
        {
            if (this.owner != null)
            {
                TabControlPlus tabControl = (TabControlPlus)this.owner;
                tabControl.Invalidate();
            }
        }

        #endregion

    }

}
